package com.zjj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjj.reggie.common.CustomExcepion;
import com.zjj.reggie.dto.SetmealDto;
import com.zjj.reggie.entity.Setmeal;
import com.zjj.reggie.entity.SetmealDish;
import com.zjj.reggie.mapper.SetmealMapper;
import com.zjj.reggie.service.SetmealDishService;
import com.zjj.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 保存套餐及商品
     * @param setmealDto 套餐及商品信息
     */
    @Transactional//事务注解
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息,操作数据库setmeal表,执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存商品信息,操作数据库setmeal_dish表,执行insert操作
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //1.查询套餐信息
        Setmeal setmeal = this.getById(id);
        //封装返回值对象
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //2.查询套餐对应的商品信息,操作数据库setmeal_dish表,执行select操作
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //1.更新套餐信息,操作数据库setmeal表,执行update操作
        this.updateById(setmealDto);
        //2.删除原有商品信息,操作数据库setmeal_dish表,执行delete操作
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(wrapper);
        //3.保存商品信息,操作数据库setmeal_dish表,执行insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void deleteWithDish(Long... id) {
        //查询套餐状态，如果状态为1，则不能删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, id);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        //如果有套餐状态为1的，则不能删除
        if (count > 0) {
            //不能删除，则抛出异常
            throw new CustomExcepion("不能删除在售的套餐");
        }
        //可以删除，则删除套餐表中的记录，商品表中的记录
        //1.删除套餐信息,操作数据库setmeal表,执行delete操作
        for (Long i : id) {
            this.removeById(i);
        }
        //2.删除套餐对应的商品信息,操作数据库setmeal_dish表,执行delete操作
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, id);
        setmealDishService.remove(wrapper);
    }


}
